package com.gdk.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdk.service.CouponService;
import com.gdk.service.PromotionService;
import com.gdk.utils.XVDateUtils;
import com.google.gson.Gson;

/**
 * 优惠券Controller
 * @author xuwei
 * time 2019/02/19 15:43
 * */


@Controller
public class CouponController {
	
	@Autowired
	CouponService couponService;
	@Autowired
	PromotionService promotionService;
	
	
	
	
	@RequestMapping("/preAddCoupon.do")
	public String preAddCoupon(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		List<Map<String, Object>> allPromotionByFlagAndStatus = promotionService.findAllPromotionByFlagAndStatus();
		request.setAttribute("allPromotionByFlagAndStatus", allPromotionByFlagAndStatus);
		
		return "jsp/coupon/coupon_add";		
	}
	
	/**
	 * 新增优惠券
	 * @author xuwei
	 * time 2019/02/19 15:43
	 * */
	@ResponseBody
	@RequestMapping("/addCoupon.do")
	public int addCoupon(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		
		
		
		
		map.put("c_creattime", XVDateUtils.getCurrenTimeMillis());
		map.put("c_btime", XVDateUtils.stringDateToMillis(map.get("c_btime").toString()));
		map.put("c_etime", XVDateUtils.stringDateToMillis(map.get("c_etime").toString()));
		System.out.println("参数："+map);
		//System.out.println(CommonUtils.StripHT(map.get("t_info").toString()));
		//下面进行文章的添加操作
		
		int addCoupon = couponService.doAddCoupon(map);
		
		return addCoupon;		
	}
	/**
	 * 查询所有优惠券
	 * @author xuwei
	 * time 2019/02/19 15:58
	 * */
	@RequestMapping("/findAllCoupon.do")
	public String findAllCoupon(HttpServletRequest request) {
		

		List<Map<String, Object>> findAllCoupon = couponService.doFindAllCoupon();
		for (Map<String, Object> map : findAllCoupon) {
			map.put("c_btime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_btime").toString()))));
			map.put("c_etime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_etime").toString()))));
			map.put("c_creattime", XVDateUtils.MillisFormat(Integer.valueOf((map.get("c_creattime").toString()))));
		}
		request.setAttribute("findAllCoupon",findAllCoupon);
		return "jsp/coupon/coupon_manage";		
	}
	/**
	 * 修改优惠券状态
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@ResponseBody
	@RequestMapping("/uodateCouponStatus.do")
	public int uodateCouponStatus(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		int status = couponService.doUpdateCouponStatus(map);

		return status;		
	}
	/**
	 * 查看优惠券详情
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@RequestMapping("/findCouponDetails.do")
	public String findCouponDetails(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		System.out.println("amp :  "+map);
		Map<String, Object> findCouponDetails = couponService.doFindCouponDetails(map);
		findCouponDetails.put("c_btime", XVDateUtils.MillisFormat(Integer.valueOf((findCouponDetails.get("c_btime").toString()))));
		findCouponDetails.put("c_etime", XVDateUtils.MillisFormat(Integer.valueOf((findCouponDetails.get("c_etime").toString()))));
		findCouponDetails.put("c_creattime", XVDateUtils.MillisFormat(Integer.valueOf((findCouponDetails.get("c_creattime").toString()))));
		
		request.setAttribute("couponDetails",findCouponDetails);
		
		List<Map<String, Object>> allPromotionByFlagAndStatus = promotionService.findAllPromotionByFlagAndStatus();
		request.setAttribute("allPromotionByFlagAndStatus", allPromotionByFlagAndStatus);
		return "jsp/coupon/coupon_update";		
	}
	
	/**
	 * 修改优惠券信息
	 * @author xuwei
	 * time 2019/02/21 09:44
	 * */
	@ResponseBody
	@RequestMapping("/updateCouponDetails.do")
	public int updateCouponDetails(@RequestParam Map<String, Object> map,HttpSession session,HttpServletRequest request) {
		

		System.out.println("参数："+map);
		
		
		map.put("c_creattime", XVDateUtils.getCurrenTimeMillis());
		map.put("c_btime", XVDateUtils.stringDateToMillis(map.get("c_btime").toString()));
		map.put("c_etime", XVDateUtils.stringDateToMillis(map.get("c_etime").toString()));
		
		
		//System.out.println(CommonUtils.StripHT(map.get("t_info").toString()));
		//下面进行文章的添加操作
		int result = couponService.doUpdateCouponDetails(map);
		
		
		return result;		
	}

}
